package com.fxkj.cloud.drms.domain.commandSettingHistory;

import com.fxkj.cloud.dbs.common.model.DeviceBasicInfo;
import com.fxkj.cloud.drms.domain.mqtt.CommandDomain;
import com.fxkj.cloud.drms.domain.mqtt.CommandKv;
import com.fxkj.common.model.BaseDomain;
import com.fxkj.common.model.HasOrganization;
import com.fxkj.common.model.UserInfo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.io.Serializable;

/**
 * <p>description: </p>
 *
 * @author ww
 * @date 2025-03-25 11:05:54
 */
@Getter
public class CommandSettingHistoryDomain extends BaseDomain implements Serializable, HasOrganization {

    /**
     * 请求id
     */
    protected UUID requestId;

    /**
     * 设备id
     */
    protected UUID deviceId;

    /**
     * 设备序列号
     */
    protected String deviceSerialNo;

    /**
     * 属性id
     */
    protected UUID attributeId;

    /**
     * 属性key
     */
    protected String attributeKey;

    /**
     * 属性值
     */
    protected String attributeValue;

    /**
     * 属性值类型
     */
    protected Integer attributeDataType;

    /**
     * 产品id
     */
    protected UUID productId;

    /**
     * 设备所属组织id
     */
    protected UUID organizationId;

    /**
     *  属性编码
     */
    protected String attributeCode;

    /**
     *  父级属性编码
     */
    protected String parentAttributeCode;

    /**
     * 编码类型
     */
    private Integer attributeType;

    private String username;

    private String deviceName;

    private String serviceName;

    private String attributeName;

    public CommandSettingHistoryDomain(UUID id, UUID requestId, UUID deviceId, String deviceSerialNo, UUID attributeId, String attributeKey, String attributeValue, Integer attributeDataType, UUID productId, Integer attributeType,
                                       String username, String deviceName, String serviceName, String attributeName,
                                       UUID organizationId, String attributeCode, String parentAttributeCode, UUID createdUser, Long createdTime, UUID updateUser, Long updateTime, Boolean deleteMark, Long deleteTime) {
        super(id, createdUser, createdTime, updateUser, updateTime, deleteMark, deleteTime);
        this.requestId = requestId;
        this.deviceId = deviceId;
        this.deviceSerialNo = deviceSerialNo;
        this.attributeId = attributeId;
        this.attributeKey = attributeKey;
        this.attributeValue = attributeValue;
        this.attributeDataType = attributeDataType;
        this.productId = productId;
        this.organizationId = organizationId;
        this.attributeCode = attributeCode;
        this.parentAttributeCode = parentAttributeCode;
        this.attributeType = attributeType;
        this.username = username;
        this.deviceName = deviceName;
        this.serviceName = serviceName;
        this.attributeName = attributeName;
    }

    public static List<CommandSettingHistoryDomain> create(CommandDomain commandDomain, UserInfo userInfo) {
        List<CommandSettingHistoryDomain> domains = new ArrayList<>();
        DeviceBasicInfo deviceBasicInfo = commandDomain.getDeviceBasicInfo();
        List<CommandKv> commandKvList = commandDomain.getCommandKvs();
        for (CommandKv commandKv : commandKvList) {
            domains.add(
                    new CommandSettingHistoryDomain(
                            null, commandDomain.getCommandId(), deviceBasicInfo.getDeviceId(), deviceBasicInfo.getSerialNo(), commandKv.attributeKey().getAttributeId(), commandKv.attributeKey().getKey(), commandKv.value(), commandKv.attributeKey().getDataType().getProtoNumber(), deviceBasicInfo.getProductId(), commandKv.attributeKey().getAttributeType(),
                            userInfo.getUsername(),commandDomain.getDeviceBasicInfo().getDeviceName(), commandKv.attributeKey().getServiceName(), commandKv.attributeKey().getAttributeName(),
                            deviceBasicInfo.getOrganizationId(), commandKv.attributeKey().getCode(), commandKv.attributeKey().getParentCode(), null, null, null, null, false, null)
            );
        }
        return domains;
    }

}
